package com.open.pokergames.main

import android.content.Intent
import android.os.Bundle
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.sinothk.hussars.parent.BaseFragment
import com.bumptech.glide.Glide
import com.jiangyy.easydialog.InputDialog
import com.open.pokergames.MainActivity
import com.open.pokergames.NetConfig
import com.open.pokergames.R
import com.open.pokergames.biz.constant.Constant
import com.open.pokergames.biz.entity.ShowInfo
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.biz.views.invite.InviteMainActivity
import com.open.pokergames.biz.views.rooms.RoomsForUserJoinGroupActivity
import com.open.pokergames.comm.repository.cache.LocalCache
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.conn.bean.WsConstant
import com.open.pokergames.texas.repository.entity.RoomEntity
import com.open.pokergames.texas.view.RoomDetailActivity
import com.open.pokergames.texas.view.cards5.Room5DetailActivity
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.home_fragment_main.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

open class HomeMainFragment : BaseFragment(), View.OnClickListener {
    private var viewModel: BizViewModel? = null
    private var currView: View? = null

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun inviteTipsForUserCallback(result: ResultInfo<ShowInfo>) {

        if ("getShowInfo" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data == null) {
                    inviteUnreadTv.visibility = View.GONE
                    inviteUnreadTv.text = ""
                } else {
                    if (result.data.inviteTips == 0) {
                        inviteUnreadTv.visibility = View.GONE
                        inviteUnreadTv.text = ""
                    } else {
                        inviteUnreadTv.visibility = View.VISIBLE
                        inviteUnreadTv.text = result.data.inviteTips.toString()
                    }

                    roomCardsTv.text = result.data.roomCards.toString()
                }
            }
            else -> {
            }
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.home_fragment_main, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = BizViewModel()
        setListener()
        initView()
    }

    override fun onResume() {
        super.onResume()
        viewModel!!.getShowInfo();
    }

    private fun initView() {
        updateHomeTop()
    }

    fun updateHomeTop() {
        try {
            val loginUser = LocalCache.getUser()
            userNameTv.text = XUtils.string().getNotNullValue(loginUser.userName)
            roomCardsTv.text = "${loginUser.roomCards}"
            Glide.with(this).load(NetConfig.getImageUrl(loginUser.userAvatar))
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .into(imageView)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun setListener() {
        banner01Btn.setOnClickListener(this)
        texasBtn.setOnClickListener(this)
        texas7Btn.setOnClickListener(this)
        startBtn.setOnClickListener(this)
        msgCenterView.setOnClickListener(this)
        inviteView.setOnClickListener(this)

        imageView.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            banner01Btn -> {

            }
            texasBtn -> {
                XUtils.intent().openActivity(activity, RoomsForUserJoinGroupActivity::class.java)
                    .putStringExtra("where", Constant.Where.MAIN_ALL)
                    .putStringExtra("title", "全张德州")
                    .startInFragment(this)
            }
            texas7Btn -> {
                XUtils.intent().openActivity(activity, RoomsForUserJoinGroupActivity::class.java)
                    .putStringExtra("where", Constant.Where.MAIN_7)
                    .putStringExtra("title", "五张德州")
                    .startInFragment(this)
            }
            startBtn -> {
                joinRoom()
            }
            msgCenterView -> {
//                XUtils.toast().show("消息中心")
            }
            inviteView -> {
                XUtils.intent().openActivity(activity, InviteMainActivity::class.java)
                    .startInFragment(this)
            }
            imageView -> {
                MainActivity.instant?.selectTab(2)
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<String>) {

        if ("getMineViewData" != result.eventType) {
            return
        }

//        refreshLayout.refreshComplete()
//
//        when (result.code) {
//            ResultInfo.SUCCESS -> {
//                showCountView(result.data)
//            }
//            ResultInfo.TOKEN_OVERDUE -> {
//            }
//            else -> {
//            }
//        }
    }

    private fun joinRoom() {
        InputDialog.Builder(activity)
            .setTitle("进入房间")
            .setInputType(InputType.TYPE_CLASS_NUMBER)
            .setHint("请输入房间号")
            .setLines(1)
            .setPositiveButton(
                "进入"
            ) { view ->
                val roomNum = view.tag.toString()
                if (XUtils.string().isEmpty(roomNum)) {
                    XUtils.toast().show("请输入房间号")
                    return@setPositiveButton
                }
                viewModel!!.retrieveRoom(roomNum)
            }
            .setNegativeButton("取消", null)
            .show()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun checkRoomEnableEventBusCallback(result: ResultInfo<RoomEntity>) {

        if ("retrieveRoom" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data.roomType == 0) {
                    XUtils.intent().openActivity(activity, RoomDetailActivity::class.java)
                        .putStringExtra("roomNum", result.data.roomNum)
                        .putStringExtra("roomName", result.data.roomName)
                        .requestCode(WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK)
                        .start()
                } else if (result.data.roomType == 1) { // 5张
                    XUtils.intent().openActivity(activity, Room5DetailActivity::class.java)
                        .putStringExtra("roomNum", result.data.roomNum)
                        .putStringExtra("roomName", result.data.roomName)
                        .requestCode(WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK)
                        .start()
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                XUtils.toast().showLong(result.msg)
            }
            else -> {
                XUtils.toast().showLong(result.msg)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK && resultCode == WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK) {
            XUtils.toast().showLong("房间已经释放")
        }
    }
}